﻿using System;
using System.Collections.Generic;
using System.Linq;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Models.Filters
{
    public class DecadeFilter : FilteredCourseFilter
    {
		#region Fields 
        private readonly List<CourseFilter> _SubFilters = new List<CourseFilter>();
		#endregion Fields 

		#region Ctor 
        public DecadeFilter(LibraryViewModel library) : base(library) { }
		#endregion

		#region Properties 
        public override string DisplayText
        {
            get { return "Дата выпуска"; }
        }

        public override bool HasSubFilters
        {
            get { return true; }
        }

        public override List<CourseFilter> SubFilters
        {
            get { return _SubFilters; }
        }
		#endregion Properties 

		#region Public Methods 
        public override void RefreshSubFilters()
        {
            _SubFilters.Clear();

            var decades = (from course in Library.Courses
                           where course.Model.YearOfPublication.HasValue
                           let decade = 10 * (int)Math.Floor((course.Model.YearOfPublication.Value / 10.0))
                           orderby decade
                           select decade)
                .Distinct();

            foreach (var decade in decades)
                _SubFilters.Add(new SingleDecadeFilter(Library, decade));
        }
		#endregion Public Methods 
    }

    public class SingleDecadeFilter : CourseFilter
    {
		#region Ctor 
        public SingleDecadeFilter(LibraryViewModel library, int year)
            : base(library)
        {
            Year = year;
        }
		#endregion

		#region Properties 
        public override string DisplayText
        {
            get { return Year + "-ые"; }
        }

        public override bool HasSubFilters
        {
            get { return false; }
        }

        public int Year { get; set; }
		#endregion Properties 

		#region Public Methods 
        public override void RefreshResults()
        {
            var results = Library.Courses.Where(item => item.Model.YearOfPublication >= Year && item.Model.YearOfPublication < (Year + 10));
            Results = new List<CourseViewModel>(results);
        }
		#endregion Public Methods 
    }

}
